package com.example.demo.service;

import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlInjectionUtils;
import com.example.demo.domain.Product;
import com.example.demo.mapper.ProductMapper;
import com.example.demo.util.SQLUtil;
import jakarta.annotation.Resource;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductService {
    @Resource
    private ProductMapper productMapper;

    private final SqlSessionFactory sqlSessionFactory;

    public ProductService(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    public void getConnectionExample(String table, String select, String where, List<Map<String, String>> data) {
        // 获取SqlSession

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            connection = sqlSession.getConnection();
            String sql = "select " + select + " from " + table + " where " + SQLUtil.replaceWhereParameters(where, data);
            if (!SqlInjectionUtils.check(sql)) {
                throw new RuntimeException("sql grammar illegal");
            }
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            List<Map<String, String>> list = new ArrayList<>();
            // 只获取一条
            if (resultSet.next()) {
                Map<String, String> map = new HashMap<>();
                for (String key : select.split(",")) {
                    String value = resultSet.getString(key);
                    map.put(key, value);
                }
                list.add(map);
            }
            if (list.size() > 0) {
                list.forEach(stringStringMap -> {
                    String s = JSONUtils.toJSONString(stringStringMap);
                    System.out.println("s = " + s);
                });
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if (resultSet!= null) resultSet.close();
                if (preparedStatement!= null) preparedStatement.close();
                if (connection!= null) connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public List<Product> queryProducts(
        String table,
        String select,
        String where,
        List<Map<String, String>> data
    ) {
        String sql = "select " + select + " from " + table + " where " + SQLUtil.replaceWhereParameters(where, data);
        return productMapper.queryProducts(sql);
    }
}